#include <stdio.h>
#include <math.h>
#include <pthread.h>

#define N 100000000
#define N_THREAD 3

struct arg_t {
        int rank;
        double local_sum;
};

void * thread_fn(void *arg) {
        struct arg_t *p_param = (struct arg_t *) arg;
        int i;
        p_param->local_sum = 0;
        for (i = p_param->rank; i < N; i+=N_THREAD) {
                p_param->local_sum += sin(i);
        }
        return NULL;
}

int main() {
        int i;
        double sum = 0;
        pthread_t tidp[N_THREAD];
        struct arg_t arg[N_THREAD];

        for (i = 0; i < N_THREAD; i++) {
                arg[i].rank = i;
                pthread_create(&tidp[i], NULL, &thread_fn, (void *)&arg[i]);
        }

        for (i = 0; i < N_THREAD; i++) {
                pthread_join(tidp[i], NULL);
                sum += arg[i].local_sum;
        }
        printf("sin(1)+...+sin(%d)=%lf\n", N-1, sum);
        return 0;
}
